package com.kwedding.util;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class Util {

	public static int getAgeByBirthday(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return getAgeByBirthday(calendar);
	}

	public static int getAgeByBirthday(Calendar date) {
		if (date == null)
			return 0;

		Calendar calendar = Calendar.getInstance();

		int loca_year = calendar.get(Calendar.YEAR);
		int loca_month = calendar.get(Calendar.MONTH);
		int loca_day = calendar.get(Calendar.DAY_OF_MONTH);

		int year = date.get(Calendar.YEAR);
		int month = date.get(Calendar.MONTH);
		int day = date.get(Calendar.DAY_OF_MONTH);

		int age = loca_year - year;
		if (loca_month < month || (loca_month == month && loca_day <= day) && age != 0)
			age -= 1;
		return age;
	}

	/**
	 * 判断邮箱是否合法
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		String format = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
		return email.matches(format);
	}

	/**
	 * 判断手机号是否合法
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isPhone(String phone) {
		String format = "(13\\d|14[57]|15[^4,\\D]|17[03678]|18\\d)\\d{8}|170[059]\\d{7}";
		return phone.matches(format);
	}

	/**
	 * 判断邮箱是否合法
	 * 
	 * @param email
	 * @return
	 */
	public static String numberToCapital(int sum) {
		if (sum == 0)
			return "日";
		else if (sum == 1)
			return "一";
		else if (sum == 2)
			return "二";
		else if (sum == 3)
			return "三";
		else if (sum == 4)
			return "四";
		else if (sum == 5)
			return "五";
		else if (sum == 6)
			return "六";
		else if (sum == 7)
			return "七";
		else if (sum == 8)
			return "八";
		else if (sum == 9)
			return "九";
		else if (sum == 10)
			return "十";
		return "";
	}

	/**
	 * @author Mickey
	 * @note 获取yyyyMMddHHmmss 时间
	 * @return
	 */
	public static String getTimeChuo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(Calendar.getInstance().getTime());
	}

	/**
	 * @author Mickey
	 * @note 获取yyyyMMddHHmmss 时间
	 * @return
	 */
	public static String getTime10Chuo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, -10);
		return sdf.format(calendar.getTime());
	}

	/**
	 * 时间转换字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * 时间转换字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 时间转换字符串
	 * 
	 * @param date
	 * @return
	 */
	public static Date stringToDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String md5(String text) {
		try {
			return md5(text.getBytes("utf-8"));
		} catch (Exception e) {

			return null;
		}
	}

	public static String md5(byte[] data) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			md5.update(data);
			byte[] digest = md5.digest();
			return encodeHex(digest);
		} catch (Exception e) {
			return null;
		}
	}

	public static String encodeHex(byte[] bytes) {
		StringBuilder buf = new StringBuilder(bytes.length + bytes.length);
		for (int i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buf.toString();
	}

	/**
	 * 生成随机验证码
	 * 
	 * @return
	 */
	public static String randomCode() {
		return String.valueOf((Math.random() + 1.0D) * 1000000.0D).substring(1, 7);
	}

	/**
	 * 生成随机字符串
	 * 
	 * @return
	 */
	public static String randomString() {

		String val = "";

		Random random = new Random();
		for (int i = 0; i < 16; i++) {
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字

			if ("char".equalsIgnoreCase(charOrNum)) // 字符串
			{
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) // 数字
			{
				val += String.valueOf(random.nextInt(10));
			}
		}
		val = val.toUpperCase();
		return val;
	}


	/**
	 * 两个ID集合取相同值
	 * 
	 * @param list1
	 * @param list2
	 * @return
	 */
	public static List<Long> getRepetition(List<Long> list1, List<Long> list2) {
		List<Long> result = new ArrayList<Long>();
		for (Long long1 : list2) {// 遍历list1
			if (list1.contains(long1)) {// 如果存在这个数
				result.add(long1);// 放进一个list里面，这个list就是交集
			}
		}
		return result;
	}
}
